import pymysql
import traceback
connection = pymysql.connect(
    host='localhost',
    user='root',
    password='cpy50686868cpy',
    database='library',
    charset='utf8'
)
c = connection.cursor()
id = 1033
n = c.execute(f'select * from book where id = %s',(id,))
print(c.fetchall())
print(f'n={n}')

# try:
#     row1 = c.execute('update book set status = 1 where id = 1045 ')
#     row2 = c.execute('insert into borrow (user_id,book_id) values (1022,1045)')
#     print(f'row1 = {row1}')
#     print(f'row2 = {row2}')
#     if row1 > 0 and row2 >0:
#         connection.commit()
#     else:
#         connection.rollback()

# except Exception:
#     print(traceback.print_exc())
#     connection.rollback()

try:
    sql = 'insert into book () values (%s,%s,%s,%s)'
    data = [(3022,'学11','英语类',0),(3056,'学22','数学类',0),(3077,'学33','xx',0)]
    rows = c.executemany(sql,data)
    print(f'插入了{rows}条语句')
    connection.commit()
except Exception:
    print(traceback.print_exc())
    connection.rollback()
c.close()
connection.close()